package com.zl.basis.service;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;

import com.zl.basis.config.BasisConfig;
import com.zl.basis.log.TimberUtil;
import com.zl.basis.network.NetWorkUtil;
import com.zl.basis.network.NetworkChangedReceiver;


public class TimeTaskService extends Service {
    private static final String TAG = "TimeTaskService";
    private static final int TIME_NET_CHECK = 1000;
    private static TimeTaskListener timeTaskListener;
    private NetworkChangedReceiver networkChangedReceiver;
    private Handler taskHandler;

    private Handler getTaskHandler() {
        if (null == taskHandler) {
            synchronized (TimeTaskService.class) {
                if (null == taskHandler) {
                    HandlerThread taskThread = new HandlerThread("TimeTaskThread");
                    taskThread.start();
                    taskHandler = new Handler(taskThread.getLooper());
                }
            }
        }
        return taskHandler;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        getTaskHandler().post(netCheckWorkTask);
        registerNetWorkChangeReceiver();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getTaskHandler().removeCallbacks(netCheckWorkTask);
        unregisterNetWorkChangeReceiver();
    }

    private void registerNetWorkChangeReceiver() {
        networkChangedReceiver = new NetworkChangedReceiver();
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(networkChangedReceiver, intentFilter);
        networkChangedReceiver.setNetWorkListener(() ->getTaskHandler().post(netCheckWorkTask));
    }

    private void unregisterNetWorkChangeReceiver() {
        if (networkChangedReceiver != null) {
            unregisterReceiver(networkChangedReceiver);
        }
    }

    private Runnable netCheckWorkTask = new Runnable() {
        @Override
        public void run() {
            boolean isOk = NetWorkUtil.getNetWorkStatus(getBaseContext());
            TimberUtil.i(TAG, "当前网络状态：" + isOk);
            BasisConfig.setNetWorkState(isOk);
            if (getTimeTaskListener() != null) {
                getTimeTaskListener().onNetStatusChange(isOk);
            }
            if (isOk) {
               getTaskHandler().postDelayed(netCheckWorkTask, TIME_NET_CHECK * 5 * 60);
            } else {
               getTaskHandler().postDelayed(netCheckWorkTask, TIME_NET_CHECK);
            }
        }
    };

    public static TimeTaskListener getTimeTaskListener() {
        return timeTaskListener;
    }

    public static void setTimeTaskListener(TimeTaskListener timeTaskListener) {
        TimeTaskService.timeTaskListener = timeTaskListener;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
